package controller

import (
	"errors"
	"ginFlutterBolg/internal/model"
	"ginFlutterBolg/internal/service"
	. "ginFlutterBolg/internal/util"
	"github.com/gin-gonic/gin"
	"strconv"
)

type (
	UserController struct {
	}
)

// UserLoginByUserName 通过用户名登录
func (u *UserController) UserLoginByUserName(c *gin.Context) {
	loginUser := new(service.LoginUser)
	if err := c.Bind(&loginUser); err != nil {
		ResponseStatusOk(c, 400, "数据同步错误", err.Error())
		return
	}

	userService := new(service.UserService)
	res, err := userService.LoginByUsername(loginUser)
	if err != nil {
		ResponseStatusOk(c, 400, "登录失败", err.Error())
		return
	}
	ResponseStatusOk(c, 200, "登录成功", res)
}

// UserLoginByAccount 通过账户登录
func (u *UserController) UserLoginByAccount(c *gin.Context) {
	loginUser := new(service.LoginUser)
	if err := c.Bind(&loginUser); err != nil {
		ResponseStatusOk(c, 400, "数据同步错误", err.Error())
		return
	}

	userService := new(service.UserService)
	res, err := userService.LoginByAccount(loginUser)
	if err != nil {
		ResponseStatusOk(c, 400, "登录失败", err.Error())
		return
	}
	ResponseStatusOk(c, 200, "登录成功", res)
}

// UserRegister 用户注册
func (u *UserController) UserRegister(c *gin.Context) {
	registerUser := new(service.RegisterUser)
	if err := c.Bind(&registerUser); err != nil {
		ResponseStatusOk(c, 400, "数据同步错误", err.Error())
		return
	}
	userService := new(service.UserService)
	user, err := userService.Register(registerUser)
	if err != nil {
		ResponseStatusOk(c, 400, "注册过程出错", err.Error())
		return
	}
	ResponseStatusOk(c, 200, "注册成功", user)

}

// GetUserInfoByToken 通过token获取信息
func (u *UserController) GetUserInfoByToken(c *gin.Context) {
	userInfo := new(service.LoginUserInfo)
	user, err := u.GetUserInfo(c)
	if err != nil {
		ResponseStatusOk(c, 400, "获取用户信息失败", err.Error())
		return
	}
	userInfo.Id = user.Id
	userInfo.Username = user.Username
	userInfo.Account = user.Account
	userInfo.Email = user.Email
	ResponseStatusOk(c, 200, "获取成功", userInfo)

}

// GetUserInfo 获取用户信息
func (u *UserController) GetUserInfo(c *gin.Context) (*model.User, error) {
	//获取用户信息
	val, exists := c.Get("userSecret")
	if !exists {
		return nil, errors.New("用户未登录")
	}
	secret := val.(string)
	if secret == "" {
		return nil, errors.New("用户登陆失败")
	}

	userService := new(service.UserService)
	user, err := userService.FindBySecret(secret)
	if err != nil {
		return nil, err
	}
	return user, nil
}

// GetUserByUid 只对路由使用
func (u *UserController) GetUserByUid(c *gin.Context) {
	uid := c.Query("Uid")
	uidInt, err := strconv.ParseInt(uid, 10, 64)
	if err != nil {
		ResponseStatusOk(c, 400, "数据绑定错误", err.Error())
		return
	}
	userService := new(service.UserService)
	user, err := userService.GetUserByUid(uidInt)
	if err != nil {
		ResponseStatusOk(c, 400, "数据查找失败", user)
		return
	}
	userInfo := map[string]interface{}{
		"name":       user.Username,
		"createTime": user.CreateTime,
		"avatar":     user.Avatar,
	}
	ResponseStatusOk(c, 200, "查找成功", userInfo)
}

// ReName 重命名
func (u *UserController) ReName(c *gin.Context) {
	newName := c.Query("new_name")
	user, err := u.GetUserInfo(c)
	if err != nil {
		ResponseStatusFail(c, 400, "请重新登录", err.Error())
		return
	}

	user.Username = newName
	userService := new(service.UserService)
	err = userService.ReName(user)
	if err != nil {
		ResponseStatusFail(c, 400, "修改失败", err.Error())
		return
	}
	ResponseStatusOk(c, 200, "修改成功", err)

}

// SearchUserByContainUsername 用户通过包含名称搜索
func (u UserController) SearchUserByContainUsername(c *gin.Context) {
	page, similarName := c.Query("page"), c.Query("name")
	pageInt, err := strconv.Atoi(page)
	if err != nil {
		ResponseStatusOk(c, 400, "参数查询错误", err.Error())
		return
	}
	userService := new(service.UserService)
	userList, err := userService.SearchByContainUsername(pageInt, similarName)
	if err != nil {
		ResponseStatusOk(c, 400, "查询失败", err.Error())
		return
	}
	ResponseStatusOk(c, 200, "ok", gin.H{
		"list": userList,
	})
}
